<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\mill\api;

use app\common\controller\ApiController;
use think\App;
use app\mill\model\TCloud as TCloudModel;
use think\Db;

/**
 * T+Cloud控制器
 * @package app\user\admin
 */
class Tcloud extends ApiController
{
    public function __construct(App $app = null)
    {
        parent::__construct($app);

    }

    /**
     * appTicket回调参数接收
     */
    public function appTicketCallback(){

        $data = file_get_contents("php://input");
        $data = json_decode($data,true);
        //AES解密
        $data = openssl_decrypt(base64_decode($data['encryptMsg']),'AES-128-ECB',TCloudModel::$config['aesKey'],1);
        $data = json_decode($data,true);
        //文件存储目录
        $file = TCloudModel::$config['file'];

        switch ($data['msgType'])
        {
            case "APP_TEST":
                //file_put_contents('test.log',$data['bizContent']['appTicket']);
                break;
            case "APP_TICKET":
                TCloudModel::fileValue($file['APP_TICKET'],$data['bizContent']['appTicket']);
                break;
            case "TEMP_AUTH_CODE":
                //file_put_contents('auth_code.log',$data['bizContent']['appTicket']);
                break;
            case "PAY_ORDER_SUCCESS":

                break;
            default:
                break;
        }

        $re = ['result'=>'success'];

        echo json_encode($re);
    }

    //获取token
    public function getToken(){

        $data = TCloudModel::getToken();

        dump($data);
    }

    //获取项目
    public function getProject(){

        $name = $order['order_sn'] = 'CS01-1';
        //录入项目
        $data = TCloudModel::projectCreate($order);
        dump($data);

        $data = TCloudModel::getProject($name);

        dump($data);
    }

    //获取员工
    public function getPerson(){

        $order['sales_code'] = "HYXT0001";
        $order['sales'] = 'CSYG';

        //查询员是否已录入，未录入则录入
        $data = TCloudModel::getPerson($order['sales']);
        //录入员
        if(empty($data)){
            $data = TCloudModel::personCreate($order);
            dump($data);
            $data = TCloudModel::getPerson($order['sales']);
        }

        dump($data);
    }

    //获取来往单位
    public function getPartner(){

        $order['company'] = 'CS01';

        //查询来往单位是否已录入，未录入则录入
        $data = TCloudModel::getPartner($order['company']);
        //录入来往单位
        if(empty($data)){
            $data = TCloudModel::partnerCreate($order);
            dump($data);
            $data = TCloudModel::getPartner($order['company']);
        }

        dump($data);
    }

    //档案号创建
    public function archivesCreate(){

        $data = [
            'archives_sn'           =>  'CS-0001',
            'class_code'            =>  '07',
            'class_name'            =>  '档案号'
        ];

        $data = TCloudModel::archivesCreate($data);

        dump($data);
    }

    //存货创建
    public function inventoryCreate(){

        $data[] = [
            'warehouse'             =>  '08',
            'class_code'            =>  '06',
            //'class_name'            =>  '成品',

            'archives_sn'           =>  'CS-0001',
            'model_sn'              =>  'XXD001',
            'one_area'              =>  0.5555,
            'page'                  =>  'CS-页码-001',
            'plank'                 =>  'CS-材质-001',
            'model'                 =>  'CS-版型-001',
            'color'                 =>  'CS-颜色-001',
            'bend'                  =>  'CS-超长-001',
            'torque'                =>  'CS-超宽-001',
            'long'                  =>  100,
            'high'                  =>  200,
            'thickness'             =>  3.0,
            'weld'                  =>  5,
            'slot'                  =>  5,
            'sort'                  =>  1001,
            'code'                  =>  'XXD001CS-0001',
        ];
        $data[] = [
            'warehouse'             =>  '08',
            'class_code'            =>  '06',
            //'class_name'            =>  '成品',

            'archives_sn'           =>  'CS-0001',
            'model_sn'              =>  'XXD002',
            'one_area'              =>  0.9555,
            'page'                  =>  'CS-页码-002',
            'plank'                 =>  'CS-材质-002',
            'model'                 =>  'CS-版型-002',
            'color'                 =>  'CS-颜色-002',
            'bend'                  =>  'CS-超长-002',
            'torque'                =>  'CS-超宽-002',
            'long'                  =>  100,
            'high'                  =>  200,
            'thickness'             =>  3.0,
            'weld'                  =>  5,
            'slot'                  =>  5,
            'sort'                  =>  1001,
            'code'                  =>  'XXD002CS-0001',
        ];

        $data = TCloudModel::inventoryCreateBatch($data);

        dump($data);
    }

    //存货更新
    public function inventoryUpdate(){

        $data = [
            'warehouse'             =>  '08',
            'class_code'            =>  '06',
            //'class_name'            =>  '成品',

            'archives_sn'           =>  'CS-0001',
            'model_sn'              =>  'XXD002',
            'one_area'              =>  1.9555,
            'page'                  =>  '1CS-页码-002',
            'plank'                 =>  '1CS-材质-002',
            'model'                 =>  '1CS-版型-002',
            'color'                 =>  '1CS-颜色-002',
            'bend'                  =>  '1CS-超长-002',
            'torque'                =>  '1CS-超宽-002',
            'long'                  =>  1100,
            'high'                  =>  1200,
            'thickness'             =>  13.0,
            'weld'                  =>  15,
            'slot'                  =>  15,
            'sort'                  =>  11001,
            'note'                  =>  '11描述',
            'extra'                 =>  '12222',
            'stiffener'             =>  '13333',
        ];

        $data = TCloudModel::inventoryUpdate($data);

        dump($data);
    }

    //查询物料清单
    public function bomQuery(){

        $data = [
            'archives_sn'           =>  'CS-0001',
        ];

        $data = TCloudModel::bomQuery($data);

        dump($data);
    }

    //创建物料清单
    public function bomCreate(){

        $data = [
            'archives_sn'           =>  'CS-0001',
            'row_area'              =>  '3',
            'lists'                 =>  [
                [
                    'code'          =>  'XXD001CS-0001',
                    'archives_sn'   =>  'CS-0001',
                    'model_sn'      =>  'XXD001',
                    'row_area'      =>  '1',
                ],
                [
                    'code'          =>  'XXD002CS-0001',
                    'archives_sn'   =>  'CS-0001',
                    'model_sn'      =>  'XXD002',
                    'row_area'      =>  '2',
                ],
            ]
        ];

        $data = TCloudModel::bomCreate($data);

        dump($data);
    }

    //审核物料清单
    public function bomAudit(){

        $data = [
            'archives_sn'           =>  'CS-0001',
        ];

        $data = TCloudModel::bomAudit($data);

        dump($data);
    }

    //查询销售订单
    public function saleOrderQuery(){

        $data['tcloud_sale_order_id'] = 4170;
        $data['archives_sn'] = '2021HY-066-258';

        $data = TCloudModel::saleOrderQuery($data);

        dump($data);
    }

    //创建销售订单
    public function saleOrderCreate(){

        $data = [
            'order_sn'              =>  'CS01-1',
            'archives_sn'           =>  'CS-0001',
            'company_code'          =>  '01010376',
            'sales_code'            =>  'HYXT0001',
            'lists'                 =>  [
                [
                    'archives_sn'   =>  'CS-0001',
                    'model_sn'      =>  'XXD001',
                    'number'        =>  '1',
                ],
                [
                    'archives_sn'   =>  'CS-0001',
                    'model_sn'      =>  'XXD002',
                    'number'        =>  '2',
                ],
                [
                    'archives_sn'   =>  'CS-0001',
                    'model_sn'      =>  'XXD003',
                    'number'        =>  '2',
                ],
                [
                    'code'          =>  'XXD002CS-0001',
                    'archives_sn'   =>  'CS-0001',
                    'model_sn'      =>  'XXD004',
                    'number'        =>  '2',
                ],
            ]
        ];

        $data = TCloudModel::saleOrderCreate($data);

        dump($data);
    }

    //创建销售出库单
    public function saleDispatchCreate(){

        $data = [
            'order_sn'              =>  'CS01-1',
            'archives_sn'           =>  'CS-0001',
            'company_code'          =>  '01010376',
            'sales_code'            =>  'HYXT0001',
            'lists'                 =>  [
                [
                    'archives_sn'   =>  'CS-0001',
                    'model_sn'      =>  'XXD001',
                    'row_area'        =>  '0.111',
                ],
                [
                    'archives_sn'   =>  'CS-0001',
                    'model_sn'      =>  'XXD002',
                    'row_area'        =>  '0.222',
                ],
                [
                    'archives_sn'   =>  'CS-0001',
                    'model_sn'      =>  'XXD003',
                    'row_area'        =>  '0.333',
                ],
                [
                    'code'          =>  'XXD002CS-0001',
                    'archives_sn'   =>  'CS-0001',
                    'model_sn'      =>  'XXD004',
                    'row_area'        =>  '0.444',
                ],
            ]
        ];

        $data = TCloudModel::saleDispatchCreate($data);

        dump($data);
    }

    //创建销货单
    public function saleDeliveryCreate(){

        $data = [
            'order_sn'              =>  'CS01-1',
            'archives_sn'           =>  'CS-0001',
            'company_code'          =>  '01010376',
            'sales_code'            =>  'HYXT0001',
            'sale_order_details_list'=>  [
                'XXD001CS-0001'     =>  1111,
                'XXD002CS-0001'     =>  1111,
                'XXD003CS-0001'     =>  1111,
                'XXD004CS-0001'     =>  1111,
            ],
            'lists'                 =>  [
                [
                    'archives_sn'   =>  'CS-0001',
                    'model_sn'      =>  'XXD001',
                    'price'         =>  '0.01',
                    'row_area'        =>  '0.3830',
                ],
                [
                    'archives_sn'   =>  'CS-0001',
                    'model_sn'      =>  'XXD002',
                    'price'         =>  '0.01',
                    'row_area'        =>  '0.7660',
                ],
                [
                    'archives_sn'   =>  'CS-0001',
                    'model_sn'      =>  'XXD003',
                    'price'         =>  '0.01',
                    'row_area'        =>  '0.4444',
                ],
                [
                    'code'          =>  'XXD002CS-0001',
                    'archives_sn'   =>  'CS-0001',
                    'model_sn'      =>  'XXD004',
                    'price'         =>  '0.01',
                    'row_area'        =>  '0.8888',
                ],
            ]
        ];

        $data = TCloudModel::saleDeliveryCreate($data);

        dump($data);
    }

    //查询形态转换单
    public function shapeVoucherQuery(){

        $data['archives_sn'] = 'BC-2023-05-0101';

        $data = TCloudModel::shapeVoucherQuery($data);

        dump($data);
    }

    //创建形态转换单
    public function shapeVoucherCreate(){

        $data = [
            'order_sn'              =>  'CS01-1',
            'archives_sn'           =>  'CS-0001',
            'company_code'          =>  '01010376',
            'sales_code'            =>  'HYXT0001',
            'lists'                 =>  [
                [
                    'archives_sn'   =>  'CS-0001',
                    'model_sn'      =>  'XXD001',
                    'price'         =>  '0.01',
                    'row_area'        =>  '0.3830',
                ],
                [
                    'archives_sn'   =>  'CS-0001',
                    'model_sn'      =>  'XXD002',
                    'price'         =>  '0.01',
                    'row_area'        =>  '0.7660',
                ],
                [
                    'archives_sn'   =>  'CS-0001',
                    'model_sn'      =>  'XXD003',
                    'price'         =>  '0.01',
                    'row_area'        =>  '0.4444',
                ],
                [
                    'code'          =>  'XXD002CS-0001',
                    'archives_sn'   =>  'CS-0001',
                    'model_sn'      =>  'XXD004',
                    'price'         =>  '0.01',
                    'row_area'        =>  '0.8888',
                ],
            ]
        ];

        $data = TCloudModel::shapeVoucherCreate($data);

        dump($data);
    }
}
